import { mock } from 'mockjs';
import type { MockMethod } from 'vite-plugin-mock';

const apis: MockMethod[] = [
  {
    // 获取组织列表
    url: '/mock/getOrganizationList',
    method: 'post',
    response: (): Service.MockServiceResult => {
      const data = mock({
        'list|20': [
          {
            organizationId: '@id',
            organizationName: '@city()@cname()车行'
          }
        ]
      });
      return {
        code: 200,
        message: 'ok',
        data: data.list
      };
    }
  },
  {
    // 获取用户列表
    url: '/mock/getUserList',
    method: 'post',
    response: (): Service.MockServiceResult => {
      const data = mock({
        'list|1000': [
          {
            userId: '@id',
            userName: '@cname',
            supplierName: '@city()@cname()科技有限公司',
            supplierId: '@increment',
            organizationName: '@city()@cname()车行',
            organizationId: '@id',
            phone:
              /^[1](([3][0-9])|([4][01456789])|([5][012356789])|([6][2567])|([7][0-8])|([8][0-9])|([9][012356789]))[0-9]{8}$/,
            'userStatus|1': ['0', '1']
          }
        ]
      });
      return {
        code: 200,
        message: 'ok',
        data: data.list
      };
    }
  },
  {
    // 获取供应商列表
    url: '/mock/getSupplierList',
    method: 'post',
    response: (): Service.MockServiceResult => {
      const data = mock({
        'list|100': [
          {
            supplierId: '@increment',
            supplierName: '@city()@cname()科技有限公司'
          }
        ]
      });
      return {
        code: 200,
        message: 'ok',
        data: data.list
      };
    }
  },
  {
    // 新增/编辑账号
    url: '/mock/editUser',
    method: 'post',
    response: (options: Service.MockOption): Service.MockServiceResult => {
      const {
        userId = undefined,
        phone = undefined,
        password = undefined,
        userName = undefined,
        supplierId = undefined,
        organizationId = undefined,
        userStatus = undefined
      } = options.body;

      if (userId === '') {
        if (!phone || !password || !userName || !supplierId || !organizationId || !userStatus) {
          return {
            code: 1000,
            message: '参数校验失败！',
            data: null
          };
        }
        return {
          code: 200,
          message: '新增账户成功！',
          data: {
            userId,
            phone,
            userName,
            supplierId,
            supplierName: '上海市宋秀兰科技有限公司',
            organizationId,
            organizationName: '天津市张三车行',
            userStatus
          }
        };
      }
      if (!phone || !userName || !supplierId || !organizationId || !userStatus) {
        return {
          code: 1000,
          message: '参数校验失败！',
          data: null
        };
      }
      return {
        code: 200,
        message: '编辑账户成功！',
        data: {
          userId,
          phone,
          userName,
          supplierId,
          supplierName: '上海市宋秀兰科技有限公司',
          organizationId,
          organizationName: '天津市张三车行',
          userStatus
        }
      };
    }
  },
  {
    // 删除账号
    url: '/mock/deleteUser',
    method: 'post',
    response: (options: Service.MockOption): Service.MockServiceResult => {
      const { userId = undefined } = options.body;
      if (!userId) {
        return {
          code: 1000,
          message: '参数校验失败！',
          data: null
        };
      }
      return {
        code: 200,
        message: '删除账号成功！',
        data: {
          userId,
          phone: '15333333333',
          userName: '张三'
        }
      };
    }
  }
];

export default apis;
